var HOST = "https://xcx.smart-zh.com/Xcx/";

function XRKHttpGet(api, succ, fail) {
  var url = HOST + api;
  wx.request({
    url: url,
    method: 'GET',
    success: function (res) {
      console.log(url, res);
      if (res.statusCode == 200) {
        succ(res.data);
      }
      else {
        fail(res.data);
      }
    },
    fail: function (error) {
      console.log(url, error);
      fail(error);
    }
  })
}
function XRKHttpPost(api, para, succ, fail) {
  var url = HOST + api;
  wx.request({
    url: url,
    data: para,
    method: 'POST',
    header: {
      "content-type": "application/x-www-form-urlencoded"
    },
    success: function (res) {
      console.log(url, res);
      if (res.statusCode == 200) {
        succ(res.data);
      }
      else {
        fail(res.data);
      }
    },
    fail: function (error) {
      console.log(url, error);
      fail(error);
    }
  })
}

function showMsgDialog(msg, cb) {
  wx.showModal({
    content: msg,
    showCancel: false,
    confirmColor: '#23a3fb',
    success: cb
  })
}
function showLoading(title = '加载中') {
  wx.showLoading({
    "title": title,
    "mask": true
  })
}

function getIndex(para, succ, fail) {
  XRKHttpPost("Index/ad", para, succ, fail);
}
function getCateList(para, succ, fail) {
  XRKHttpPost("Goods/cate_list", para, succ, fail);
}
function getAllCateList(para, succ, fail) {
  XRKHttpPost("Goods/cate_all", para, succ, fail);
}
function getGoodsList(para, succ, fail) {
  XRKHttpPost("Goods/goods_list", para, succ, fail);
}
function getRecommentList(para, succ, fail) {
  XRKHttpPost("Goods/recomment_list", para, succ, fail);
}
function getGoodsDetail(para, succ, fail) {
  XRKHttpPost("Goods/goods_detail", para, succ, fail);
}
function getNewsDetail(para, succ, fail) {
  XRKHttpPost("Goods/news_detail", para, succ, fail);
}
function setReadList(para, succ, fail) {
  XRKHttpPost("Goods/read_list", para, succ, fail);
}
function login(para, succ, fail) {
  XRKHttpPost("Login/login", para, succ, fail);
}
function showMsgDialog(msg, cb) {
  wx.showModal({
    content: msg,
    showCancel: false,
    confirmColor: '#23a3fb',
    success: cb
  })
}
function showLoading(title = '加载中') {
  wx.showLoading({
    "title": title,
    "mask": true
  })
}
function checkAuth(fun) {
  wx.getSetting({
    /*success(res) {
    }*/
    success: fun
  })
}
module.exports = {
  getIndex: getIndex,
  getCateList: getCateList,
  getAllCateList: getAllCateList,
  getRecommentList: getRecommentList,
  getGoodsList: getGoodsList,
  getNewsDetail: getNewsDetail,
  getGoodsDetail: getGoodsDetail,
  setReadList: setReadList,
  checkAuth: checkAuth,
  showMsgDialog: showMsgDialog,
  showLoading: showLoading,
  login: login
}